var BangoStorage = function()
{

    var pub = {};
    var _bangoStore;
    var _keyName = "BangoStore";
    var _profileId = -1;

    pub.getBatch = function()
    {
        return getProfile(_profileId);
    }

    pub.bangoStore = function()
    {
        return _bangoStore;
    }

    pub.profileId = function()
    {
        return _profileId;
    }

    pub.initStorage = function(useOfflineStorage)
    {
        var savedData;
        if (useOfflineStorage)
    	{
            try
            {
	            savedData = widget.preferenceForKey(_keyName);
	            if ((savedData) && (savedData != null) && (savedData != undefined))
	            {
                	_bangoStore = JSON.parse(savedData);
	            }
            }
            catch (ex)
            {
            }
        }

        if (_bangoStore == null)
        {
            _bangoStore = new Array();
        }
    };

    pub.saveStore = function(useOfflineStorage)
    {
        var dataToSave;
        if (useOfflineStorage)
        {
            try
            {
                dataToSave = JSON.stringify(_bangoStore);
                widget.setPreferenceForKey(dataToSave, _keyName);
            }
            catch (ex)
            {

            }
        }

    };

    function getProfile(profileId)
    {
        var profile = null;

        if (_bangoStore != null)
        {
            for (profileCounter = 0; profileCounter < _bangoStore.length; profileCounter++)
            {
                if (profileId == _bangoStore[profileCounter].profileId)
                {
                    profile = _bangoStore[profileCounter];
                    break;
                }
            }
        }
        return profile;
    };

    pub.logProfile = function(useOfflineStorage, sdkType, sdkVersion, applicationId, applicationType, applicationChannel, applicationVersion, userId, sessionRestartTime, connectionType, isRoaming, locationSource, locationAltitude, locationLatitude, locationLongitude, locationAccuracy, operatorCode, operatorName, operatorCountryIso, simOperator, brand, device, model, product, version, buildId, operatorConnectionType)
    {
        var profileId = -1;

        if (applicationId == null)
        {
            applicationId = "";
        }

        if (applicationType == null)
        {
            applicationType = "";
        }

        if (applicationChannel == null)
        {
            applicationChannel = "";
        }

        if (applicationVersion == null)
        {
            applicationVersion = "";
        }

        if (userId == null)
        {
            userId = "";
        }

        if (locationSource == null)
        {
            locationSource = "";
        }

        if (operatorCode == null)
        {
            operatorCode = "";
        }

        if (operatorName == null)
        {
            operatorName = "";
        }

        if (operatorCountryIso == null)
        {
            operatorCountryIso = "";
        }

        if (simOperator == null)
        {
            simOperator = "";
        }

        if (brand == null)
        {
            brand = "";
        }

        if (device == null)
        {
            device = "";
        }

        if (model == null)
        {
            model = "";
        }

        if (product == null)
        {
            product = "";
        }

        if (version == null)
        {
            version = "";
        }

        if (buildId == null)
        {
            buildId = "";
        }

        profileId = 0;
        if (_bangoStore.length > 0)
        {
            for (storedProfileIndex = 0; storedProfileIndex < _bangoStore.length; storedProfileIndex++)
            {
                if (_bangoStore[storedProfileIndex].profileId > profileId)
                {
                    profileId = _bangoStore[storedProfileIndex].profileId;
                }
            }
        }

        profileId++;
        _bangoStore.push(new BangoProfile(profileId, sdkType, sdkVersion, applicationId, applicationType, applicationChannel, applicationVersion, userId, sessionRestartTime, connectionType, isRoaming, locationSource, locationAltitude, locationLatitude, locationLongitude, locationAccuracy, operatorCode, operatorName, operatorCountryIso, simOperator, brand, device, model, product, version, buildId, operatorConnectionType));
        _profileId = profileId;
        this.saveStore(useOfflineStorage);
    };

    pub.logEvent = function(useOfflineStorage, connectionType, operatorConnectionType, errorLevel, eventName, eventValue, eventDetail, errorId, eventTime, eventLocalTime, eventParameters)
    {
        var profile = null;
        var eventId = -1;

        if (eventName == null)
        {
            eventName = "";
        }

        if (eventDetail == null)
        {
            eventDetail = "";
        }

        if (eventValue == null)
        {
            eventValue = "";
        }

        if (errorId == null)
        {
            errorId = "";
        }

        profile = getProfile(_profileId);
        if (profile != null)
        {
            eventId = profile.Events.length + 1;
            profile.Events.push(new BangoEvent(_profileId, eventId, connectionType, operatorConnectionType, errorLevel, eventName, eventValue, eventDetail, errorId, eventTime, eventLocalTime, eventParameters));
			this.saveStore(useOfflineStorage);
        }
        return eventId;
    };

    pub.clearIsPostingFlag = function(useOfflineStorage, profileForPost)
    {
        for (eventCounter = 0; eventCounter < profileForPost.Events.length; eventCounter++)
        {
            profileForPost.Events[eventCounter].EventIsPosting = false;
        }
        this.saveStore(useOfflineStorage);

    };

    pub.updateEventsPosted = function(useOfflineStorage, profileForPost)
    {
		for (eventCounter = 0; eventCounter < profileForPost.Events.length; eventCounter++) {
			profileForPost.Events[eventCounter].EventIsPosting = false;
			profileForPost.Events[eventCounter].EventPosted = true;
		}
    	this.saveStore(useOfflineStorage);
    };

    pub.deleteEventsPosted = function(useOfflineStorage, profileId)
    {
        var returnValue = -1;
        if (_bangoStore != null && profileId != _profileId)
        {
            for (profileCounter = 0; profileCounter < _bangoStore.length; profileCounter++)
            {
                if (profileId == _bangoStore[profileCounter].profileId)
                {
                    _bangoStore.splice(profileCounter, 1);
                    this.saveStore(useOfflineStorage);
                    break;
                }
            }
        }
    };

    return pub;
}();
